package com.loongxun.playwithyou.network;

import android.content.Context;

import com.loongxun.playwithyou.ApiInterface;
import com.loongxun.playwithyou.App;
import com.loongxun.playwithyou.util.ToastView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.FileAsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * Created by Administrator on 2017/7/13.
 */
public class nHttpClient {

//    private static AsyncHttpClient asyncHttpClient = new SyncHttpClient();
    private static AsyncHttpClient syncHttpClient = new AsyncHttpClient();

    /**
     * post请求带参数
     * @param myHttpClientRespenseHandler
     */
    private static void post(String url,RequestParams requestParams, MyHttpClientRespenseHandler myHttpClientRespenseHandler) {
        getClient().setTimeout(20000);
        getClient().post(ApiInterface.SERVER_PRODUCTION + url, requestParams, myHttpClientRespenseHandler);
    }

    /**
     * post请求带参数
     * @param myHttpClientRespenseHandler
     */
    private static void post(String url,RequestParams requestParams, MyHttpRespenseHandler myHttpClientRespenseHandler) {
        getClient().setTimeout(20000);
        getClient().post(ApiInterface.SERVER_PRODUCTION + url, requestParams, myHttpClientRespenseHandler);
    }

    /**
     * post请求不带参数
     * @param myHttpClientRespenseHandler
     */
    private static void post(String url,MyHttpClientRespenseHandler myHttpClientRespenseHandler) {
        syncHttpClient.setTimeout(20000);
        syncHttpClient.post(ApiInterface.SERVER_PRODUCTION + url, myHttpClientRespenseHandler);
    }

    /**
     * get请求
     * @param myHttpClientRespenseHandler
     */
    private static void get(String url, MyHttpClientRespenseHandler myHttpClientRespenseHandler) {
        getClient().setTimeout(20000);
        getClient().get(ApiInterface.SERVER_PRODUCTION + url, myHttpClientRespenseHandler);
    }

    /**
     * get请求
     * @param myHttpClientRespenseHandler
     */
    private static void get(String url,RequestParams requestParams, MyHttpClientRespenseHandler myHttpClientRespenseHandler) {
        getClient().setTimeout(20000);
        getClient().get(ApiInterface.SERVER_PRODUCTION + url,requestParams, myHttpClientRespenseHandler);
    }

    /**
     *
     * @return
     */
    private static AsyncHttpClient getClient() {
//        if (Looper.myLooper() == null) {
            return syncHttpClient;
//        } else {
//            return asyncHttpClient;
//        }
    }

    /**
     * 取消请求（根据context）
     */
    public static void cancelRequest(Context context) {
        getClient().cancelRequests(context, true);
    }

    /**
     * 获取数据
     */
    public static void getAll(String url,String params ,MyHttpClientRespenseHandler myHttpClientRespenseHandler ) {
        Boolean net = App.isNetworkConnected();
        if (!net){
            new ToastView(App.context,"网络连接不可用，请稍后重试！").show();
        }else {
            RequestParams requestParams=new RequestParams();
            requestParams.put("json", params);
            post(url, requestParams, myHttpClientRespenseHandler);
        }
    }

    public static void getAll(String url,RequestParams params ,MyHttpRespenseHandler myHttpClientRespenseHandler ) {
        post(url,params,myHttpClientRespenseHandler);
    }

    /**
     * 下载文件方式1
     * @param url
     */
    public static void downloadFile(Context context, String url, BinaryHttpResponseHandler response) {
        getClient().get(context, url, response);
    }

    /**
     * 下载文件方式2
     * @param url
     */
    public static void downLoadFile2(Context context,String url,FileAsyncHttpResponseHandler fileAsyncHttpResponseHandler) {
        getClient().get(context, url, fileAsyncHttpResponseHandler);
    }
}
